package fons.util;

import java.util.Collections;
import java.util.List;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.common.collect.Lists;

public class LiteralExtractor {
	public class Literal {
		public final double number;
		public final int start;
		public final int end;

		public Literal (double number, int start, int end) {
			this.number = number;
			this.start = start;
			this.end = end;
		}
	}

	private final String sentence;
	private final List<Literal> numbers = Lists.newArrayList();

	public LiteralExtractor(String from) {
		this.sentence = from;

		extract();
	}

	private void extract() {
		Pattern p = Pattern.compile("-?(([0-9]+\\.[0-9]*)|([0-9]*\\.[0-9]+)|([0-9]+))");
		Matcher m = p.matcher(sentence);
		while (m.find()) {
			final MatchResult result = m.toMatchResult();
			final double value = Double.parseDouble(result.group());
			numbers.add(new Literal(value, result.start(), result.end()));
		}
	}
	
	public List<Literal> getNumbers() {
		return Collections.unmodifiableList(this.numbers);
	}
	
	public Literal getFirstNumberAfter (int position) {
		for (Literal entry: numbers) {
			if (entry.start >= position)
				return entry;
		}
		return null;
	}
	
	public static void main (String[] args) {
		
	}

}
